#include "ui.h"
#include "../components/dispaly/dispaly.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <stdarg.h>
#include <string.h>
#include "../components/mpu6050/mpu6050.h"

u8g2_t ui;

typedef struct
{
    void (*update)(void);
    uint8_t update_flag;
}Page_t;

u8g2_t ui;
static uint8_t g_page_type = PAGE_MSG;

#define MSG_BUG_HOR_LEN (21)
#define MSG_BUG_VER_LEN (3)
#define MSG_BUG_VER_PIXEL (10)
#define MSG_BUF_SIZE   (MSG_BUG_HOR_LEN*MSG_BUG_VER_LEN)
static char msg_buf[MSG_BUF_SIZE] = {0};
static void page_msg(void);


#define STATUS_HOR_LEN (21)
#define STATUS_VER_LEN (3)
#define STATUS_VER_PIXEL (10)
static void page_show_status(void);
static void page_show_start_page(void);

Page_t pages[] = 
{
    {page_msg,0},
    {page_show_status, 0},
    {page_show_start_page, 0}
};

static void page_msg(void)
{
    char buf[MSG_BUG_HOR_LEN+1];
    char *dis_buf_p = msg_buf;
    uint8_t len = strlen(dis_buf_p)+1;
    uint8_t left_len = len;
    uint8_t dis_len = 0;
    uint8_t y_offset = MSG_BUG_VER_PIXEL;
    while(left_len)
    {
        dis_len = 0;
        for(uint8_t i=0; i<left_len; i++)
        {
            
            if(i >= MSG_BUG_HOR_LEN)
            {
                break;
            }
            else if(dis_buf_p[i] == '\n')
            {
                dis_len++;
                break;
            }
            buf[dis_len++] = dis_buf_p[i];
        }
        buf[dis_len] = '\0';
        u8g2_DrawStr(&ui, 0,y_offset,buf);
        y_offset += MSG_BUG_VER_PIXEL;
        left_len -= dis_len;
        dis_buf_p += dis_len;
    }
}

void page_show_status(void)
{
    uint8_t x=0, y = STATUS_VER_PIXEL;
    pages[PAGE_STATUS].update_flag = 1; /*always update*/
    char buf_line[STATUS_HOR_LEN];
    // snprintf(buf_line, STATUS_HOR_LEN,"motor: L=%d R=%d", motor(MOTOR_LEFT)->speed_get, motor(MOTOR_RIGHT)->speed_get);
    snprintf(buf_line, STATUS_HOR_LEN,"motor: L=%d R=%d", 0, 0);
    u8g2_DrawStr(&ui, 0,y,buf_line);
    snprintf(buf_line, STATUS_HOR_LEN,"pitch: %.2f", mpu6050()->euler.pitch);
    y += STATUS_VER_PIXEL;
    u8g2_DrawStr(&ui, 0,STATUS_VER_PIXEL+STATUS_VER_PIXEL,buf_line);
    y += STATUS_VER_PIXEL;
    int pos = mpu6050()->euler.pitch;
    if(pos > 49)
    {
        pos = 49;
    }
    else if(pos < -49)
    {
        pos = -49;
    }
    x = pos+49;
    u8g2_DrawStr(&ui, x,y,"(*v*)");
}

void page_show_start_page(void)
{
    pages[PAGE_STATUS].update_flag = 1; /*always update*/
    u8g2_DrawBox(&ui, 0, 0, 10,10);
}

void dispaly_msg(char* format, ...)
{
    va_list ap;
    va_start(ap, format);
    vsnprintf(msg_buf, MSG_BUF_SIZE, format, ap);
    pages[PAGE_MSG].update_flag = 1;
}

void dispaly_process(void)
{
    if(pages[g_page_type].update_flag)
    {
        pages[g_page_type].update_flag = 0;
        u8g2_FirstPage(&ui);
        do
        {
            pages[g_page_type].update();
        } while (u8g2_NextPage(&ui));
    }
}

void dispaly_page_set(Page_tyte page_type)
{
    switch(page_type)
    {
        case PAGE_MSG:
            g_page_type = page_type;
            u8g2_SetFont(&ui, u8g2_font_6x12_tr);
            pages[g_page_type].update_flag = 1;
            break;
        case PAGE_STATUS:
            g_page_type = page_type;
            u8g2_SetFont(&ui, u8g2_font_6x12_tr);
            pages[g_page_type].update_flag = 1;
            break;
        case PAGE_START:
            g_page_type = page_type;
            pages[g_page_type].update_flag = 1;
        default:
            break;
        
    }
}

void ui_task(void *pv)
{
    printf("ui_task\n");
    u8g2_for_128x32_init(&ui);
    dispaly_page_set(PAGE_STATUS);
    while(1)
    {
        dispaly_process();
        vTaskDelay(30);
    }
}